/**
 * service_tournote_comment.js file;
 * company:junqitecch
 * author:xuzuiotao
 * handling response information
 */


var mongo_comment = require('../mongo/tournote_comment');
var cache=require('../redis/usercache');
var  consts  = require('../consts');

/*
 * 评论游迹
 */
function processComment(request,response,callback){
	
	var data =request.data;
	if(request.token===null || typeof(request.token)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}
	
	
	if(data===null || typeof(data)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;

	}else {
		console.log(data);
	}
    var  param = null;
    
    try {
    	param = JSON.parse(data);
	} catch (e) {
	}

	if(param===null){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;
	}

	if(param.contID ===null || typeof(param.contID)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':contID';
		if(callback){
			callback(response);
		}
		return false;
	}
	
	if(param.type ===null || typeof(param.type)==='undefined'){
	    param.type=2;
	}
	
	cache.getByToken(request.token,function(tk,entity){
		if(tk===null | typeof(tk)==='undefined'){
			response.errorCode=consts.Error.NEED_LOGIN;
			response.message=consts.Message.NEED_LOGIN;
			if(callback){
				callback(response);
			}
			return;
		}
		
		mongo_comment.insert(param,entity,null,function (error,obj){
		    if(error !==null){
		    	response.errorCode=error.number;
				response.message= error.message;
				if(callback){
					callback(response);
				}
				return;
		    }
		    	
			response.errorCode = consts.Error.NONE_ERROR;
			response.message= consts.Message.OK_MESSAGE;
		
			response.data=JSON.stringify(obj);

			if(callback){
				callback(response);
			}
		 });
	
	});

}


/*
 * 点赞游迹
 */
function processPraise(request,response,callback){
	
	var data =request.data;
	if(request.token===null || typeof(request.token)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}
	
	
	if(data===null || typeof(data)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;

	}else {
		console.log(data);
	}
    var  param = null;
    
    try {
    	param = JSON.parse(data);
	} catch (e) {
	}

	if(param===null){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;
	}

	if(param.contID ===null || typeof(param.contID)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':contID';
		if(callback){
			callback(response);
		}
		return false;
	}
	
	if(param.cancel ===null || typeof(param.cancel)==='undefined'){
	    param.cancel=false;
	}
	
	cache.getByToken(request.token,function(tk,entity){
		if(tk===null | typeof(tk)==='undefined'){
			response.errorCode=consts.Error.NEED_LOGIN;
			response.message=consts.Message.NEED_LOGIN;
			if(callback){
				callback(response);
			}
			return;
		}
		
		mongo_comment.praise(param,entity,function (error,obj){
		    if(error !==null){
		    	response.errorCode=error.number;
				response.message= error.message;
				if(callback){
					callback(response);
				}
				return;
		    }
		    	
			response.errorCode = consts.Error.NONE_ERROR;
			response.message= consts.Message.OK_MESSAGE;
			response.data=JSON.stringify(obj);
			if(callback){
				callback(response);
			}
		 });
	
	});

}


/*
 * 删除评论
 */
function processCommentDel(request,response,callback){
	
	console.log('mmmmm');
	var data =request.data;
	if(request.token===null || typeof(request.token)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}
	
	if(data===null || typeof(data)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;

	}else {
		console.log(data);
	}
    var  param = null;
	console.log('mmmmm222222');

    try {
    	param = JSON.parse(data);
	} catch (e) {
	}

	if(param===null){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;
	}

	if(param.contID ===null || typeof(param.contID)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':contID';
		if(callback){
			callback(response);
		}
		return false;
	}
	console.log('mmmmm3333333');

	if(param._id ===null || typeof(param._id)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':_id';
		if(callback){
			callback(response);
		}
		return false;
	}
	
	cache.getByToken(request.token,function(tk,entity){
		if(tk===null || typeof(tk)==='undefined'){
			response.errorCode=consts.Error.NEED_LOGIN;
			response.message=consts.Message.NEED_LOGIN;
			if(callback){
				callback(response);
			}
			return;
		}
		console.log('mmmmm4444444');

		mongo_comment.commentDel(param,entity,function (error,obj){
		    if(error !==null){
		    	response.errorCode=error.number;
				response.message= error.message;
				if(callback){
					callback(response);
				}
				return;
		    }
			console.log('mmmmm55555');
  	
			response.errorCode = consts.Error.NONE_ERROR;
			response.message= consts.Message.OK_MESSAGE;
			response.data = JSON.stringify(obj);
			if(callback){
				callback(response);
			}
		 });
	
	});

}

/*
 * 评论列表
 */
function processCommentList(request,response,callback){
	
	var data =request.data;
    
	var count =20;
	var baseLine=0;
	var direction = -1;
	var param = null;
    try {
        param = JSON.parse(data);    	
	} catch (e) {
		
	}
	
	if(param===null){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;
	}

	if(param.contID ===null || typeof(param.contID)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':contID';
		if(callback){
			callback(response);
		}
		return false;
	}
	
	
	
	if(param!==null){
		if(param.baseLine !==null && typeof(param.baseLine)!=='undefined'){
			baseLine = param.baseLine;
		}
		if(param.count !==null && typeof(param.count)!=='undefined'){
			count = param.count;
		}	
		if(param.direction !==null && typeof(param.direction)!=='undefined'){
			direction = param.direction;
		}
	}

	mongo_comment.listCommentCommand(param.contID, baseLine,direction,count,function (error,obj){
	    	if(error !==null){
	    		response.errorCode=error.number;
				response.message= error.message;
				if(callback){
					callback(response);
				}
				return;
	    		
	    	}
	    	
			response.errorCode = consts.Error.NONE_ERROR;
			response.message= consts.Message.OK_MESSAGE;
			response.data = JSON.stringify(obj);	
			if(callback){
				callback(response);
			}
	 });
}

module.exports={
		processComment:processComment,
		processCommentDel:processCommentDel,
		processCommentList:processCommentList,
		processPraise:processPraise,
	};

